{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#COLS = train_val_data[4]\n",
    "COLS = ['duoyin',\n",
    " 'qing',\n",
    " 'yin',\n",
    " 'yu',\n",
    " 'hour',\n",
    " 'gfrl',\n",
    " 'cur_a',\n",
    " 'cur_b',\n",
    " 'cur_c',\n",
    " 'vol_a',\n",
    " 'vol_b',\n",
    " 'vol_c',\n",
    " 'p',\n",
    " 'szgl',\n",
    " 'low_tp',\n",
    " 'high_tp',\n",
    " 'avg_tp',\n",
    " 'wind']\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm.notebook import tqdm\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "import tensorflow.keras.backend as K\n",
    "from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n",
    "from tensorflow.keras.callbacks import LearningRateScheduler, ReduceLROnPlateau\n",
    "from tensorflow.keras.optimizers.schedules import ExponentialDecay\n",
    "from sklearn.metrics import mean_absolute_error as mae\n",
    "from sklearn.preprocessing import RobustScaler, normalize\n",
    "from sklearn.model_selection import train_test_split, GroupKFold, KFold\n",
    "from IPython.display import display\n",
    "\n",
    "COMPUTE_LSTM_IMPORTANCE = 1\n",
    "ONE_FOLD_ONLY = 1\n",
    "NUM_FOLDS = 2\n",
    "gpu_strategy = tf.distribute.get_strategy()\n",
    "\n",
    "with gpu_strategy.scope():\n",
    "    # 执行预测函数\n",
    "    kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=100)\n",
    "    test_preds = []\n",
    "    for fold, (train_idx, test_idx) in enumerate(kf.split(train_val_data[0], train_val_data[2])):\n",
    "        K.clear_session()\n",
    "        print('-'*15, '>', f'Fold {fold+1}', '<', '-'*15)\n",
    "        X_train, X_valid = train_val_data[0][train_idx], train_val_data[0][test_idx]\n",
    "        y_train, y_valid = train_val_data[2][train_idx], train_val_data[2][test_idx]\n",
    "        # 导入已经训练好的模型\n",
    "        model = keras.models.load_model('./model/lstm_user')\n",
    "        # 计算特征重要性\n",
    "        if COMPUTE_LSTM_IMPORTANCE:\n",
    "            results = []\n",
    "            print(' Computing LSTM feature importance...')\n",
    "            for k in tqdm(range(len(COLS))):\n",
    "                if k>0: \n",
    "                    save_col = X_valid[:,:,k-1].copy()\n",
    "                    np.random.shuffle(X_valid[:,:,k-1])   \n",
    "                oof_preds = model.predict(X_valid, verbose=0).squeeze() \n",
    "                mae = np.mean(np.abs( oof_preds-y_valid ))\n",
    "                results.append({'feature':COLS[k],'mae':mae})\n",
    "                if k>0: \n",
    "                    X_valid[:,:,k-1] = save_col\n",
    "            # 展示特征重要性\n",
    "            df = pd.DataFrame(results)\n",
    "            df = df.sort_values('mae')\n",
    "            plt.figure(figsize=(10,20))\n",
    "            plt.barh(np.arange(len(COLS)),df.mae)\n",
    "            plt.yticks(np.arange(len(COLS)),df.feature.values)\n",
    "            plt.title('LSTM Feature Importance',size=16)\n",
    "            plt.ylim((-1,len(COLS)))\n",
    "            plt.show()\n",
    "\n",
    "            # SAVE LSTM FEATURE IMPORTANCE\n",
    "            #df = df.sort_values('mae',ascending=False)\n",
    "            #df.to_csv(f'lstm_feature_importance_fold_{fold}.csv',index=False)\n",
    "\n",
    "        # ONLY DO ONE FOLD\n",
    "        if ONE_FOLD_ONLY:break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
